var checking_hot;
var checking_pageNavObj = null;
var checking_current=0;
var checking_item_id;
$(".nav_check").on("click",function () {
    if(!checking_hot){
        getCheckingAll(0);
    }
});
$(".checking_close").on("click",function () {
    $("#checking_remark").val("");
    $("#checking_meetingName").val("");
});
$(".checking_submit").on("click",function () {
    var remark=$.trim($("#checking_remark").val()).replace(/[\r\n]/g,"");
    var meetingName=$.trim($("#checking_meetingName").val());
    var status=$("input[name='status']:checked").val();
    if(remark!=""&&meetingName!=""){
        var params=JSON.stringify({"attendRecordId":checking_item_id,"statusCode":status,"remark":remark,"name":meetingName});
        $.ajax({
            // url:'http://125.65.40.171/conference-check/attendRecordController/editIsApproved',
            url:'attendRecordController/editIsApproved',
            dataType:'json',
            data:{"param":params},
            type:"post",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success:function (data) {
                if(data.code==200){
                    window.location.href="manage";
                }else if(data.code==500){
                    alert("操作失败");
                }
            },
            error:function (XMLHttpRequest,textStatus,errorThrown) {
                alert('系统错误');
            }
        })
    }else{
        alert("请输入会议名称或者备注");
    }

});
function createCheckingTable(datas,num) {
    if(datas!=null){
        var data=[];
        $.each(datas,function (e,v) {
            var list={"name":v.name,"phoneNumber":v.phoneNumber,"payAccount":v.payAccount,"amountOfPay":v.amountOfPay,"conferenceName":v.conferenceName,"cost":v.cost,"reason":v.reason,"isApproved":v.isApproved,"id":v.id};
            data.push(list);
        });
        if(num==0){
            var radio=function(instance, td, row, col, prop, value, cellProperties){
                Handsontable.renderers.TextRenderer.apply(this, arguments);
                if(col==7){
                    if(value==0){
                        td.innerHTML="未通过";
                    }else if(value==2){
                        td.innerHTML="审核中";
                    }
                }else if(col==8){
                    td.innerHTML="";
                    var div=$("<div><button type='button' checking_id='"+value+"' class='btn btn-default checking_operation' data-toggle='modal' data-target='#checkingOperationBody'>审核</button></div>");
                    $(td).append(div);
                    $(".checking_operation").on("click",function () {
                        checking_item_id=$(this).attr("checking_id");
                    });
                }
            };
            var container_checking = document.getElementById('table_checking');
            checking_hot = new Handsontable(container_checking, {
                data: data,
                autoColumnSize:true,
                rowHeaders: false,
                colHeaders: ["用户名","手机号码","打款账号","打款金额","会议名称","参会费用","备注","状态","操作"],
                fillHandle:true,
                cells: function (row, col, prop){
                    var cellProperties = {};
                    if(row>=0){
                        cellProperties.renderer = radio;
                    }
                    cellProperties.readOnly = true;
                    return cellProperties;
                }
            });
            $("#table_checking").siblings("#hot-display-license-info").css("display","none");
        }else{
            checking_hot.loadData(data);
            checking_current=num;
        }
    }
}
// 获取所有数据
function getCheckingAll(num) {
    var par=0;
    if(num==0){
        par=1;
    }else{
        par=num;
    }
    var params=JSON.stringify({"pageNumber":par});
    $.ajax({
        // url:'http://125.65.40.171/conference-check/attendRecordController/selectAllAttendRecordUnApproved',
        url:'attendRecordController/selectAllAttendRecordUnApproved',
        dataType:'json',
        data:{"param":params},
        type:"post",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success:function (data) {
            if(data.code==200){
                createCheckingTable(data.data.dataList,num);
                // 初始化分页
                if(checking_current==num){
                    checking_pageNavObj = new PageNavCreate("checking_PageNavId",{
                        pageCount:data.data.pageTotal,//总页数
                        currentPage:par,//当前页
                        perPageNum:5//每页按钮数
                    });
                    checking_pageNavObj.afterClick(checkingPageNavCallBack);
                }
            }else if(data.code==500){
                createCheckingTable(null,num);
                alert("当前没有数据");
            }
        },
        error:function (XMLHttpRequest,textStatus,errorThrown) {
            alert('系统错误');
        }
    })
}
//翻页按钮点击后触发的回调函数
function checkingPageNavCallBack(clickPage){
    // console.log(clickPage);
    // 更新分页插件
    // pageNavCreate("PageNav",pageCount,clickPage,pageNavCallBack);
    checking_pageNavObj = new PageNavCreate("checking_PageNavId",{
        pageCount:getPageSet().pageCount,//总页数
        currentPage:clickPage,//当前页
        perPageNum:getPageSet().perPageNum,//每页按钮数
    });
    checking_pageNavObj.afterClick(checkingPageNavCallBack);
    if(checking_current!=clickPage){
        getCheckingAll(clickPage);
    }
}
